package com.chunsoft.intent_service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private TextView textView1;
    private MyService myService = null;
    private MyService.LocalBinder localBinder;

    private boolean flag = false;
    private String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainxml);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        textView1 = (TextView) findViewById(R.id.textView1);


        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag)
                {
                    int value = myService.getRandom();
                    textView1.setText("" + value);
                }
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(connection);
                flag = false;
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //向Sercice中传递数据
                Parcel parcel = Parcel.obtain();
                parcel.writeInt(23);
                parcel.writeString("jack");
                //从Service中获取数据
                Parcel reply = Parcel.obtain();
                try {
                    localBinder.transact(IBinder.LAST_CALL_TRANSACTION,parcel,reply,0);

                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                Log.e(TAG, "----从Service中回传的值－－－－>" + reply.readInt());
                Log.e(TAG, "----从Service中回传的值－－－－>"+reply.readString());
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

        Intent intent = new Intent(this, MyService.class);
        bindService(intent, connection, Context.BIND_AUTO_CREATE);
    }

    public ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            localBinder = (MyService.LocalBinder) service;

            myService = localBinder.getService();
            flag = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            flag = false;
        }
    };

    @Override
    public void onStop() {
        super.onStop();
    }
}
